package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author kabuda
 * @create 2022-09-07 14:39
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
   RedisTemplate redisTemplate;
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //判断手机号是否为空
        String phone = user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            //生成验证码
            Integer code = ValidateCodeUtils.generateValidateCode(4);
            //腾讯云发送验证码
            //String[] phoneNumbers=new String[]{phone};
            //String[] templateParams=new String[]{code.toString()};
            //SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,phoneNumbers,templateParams);
            log.info(code.toString());
            //存储验证码到session
            //session.setAttribute(phone,code);
            //存入redis，5分钟失效
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.success("发送成功");
        }
        return R.success("发送失败");
    }
    @PostMapping("/login")
    public R<String> login(@RequestBody Map map, HttpSession session){
        String phone = (String) map.get("phone");
        String code = (String) map.get("code");
        //Object code1 =  session.getAttribute(phone);
        //从redis获取验证码比对
        Object code1=redisTemplate.opsForValue().get(phone);
        String code2 = code1.toString();
        if(code!=null&&code2!=null&&code.equals(code2)){
            //如果是新用户，自动注册
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User user = userService.getOne(queryWrapper);
            if(user==null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            }
            session.setAttribute("user",user.getId());
            //登录成功删除验证码
            redisTemplate.delete(phone);
            return R.success("登录成功");
        }
        return R.success("登录失败");
    }
}
